/*     author: YangMingWei   time ：2018/6/13
*    describe: 绑定td的拖动右侧自定义宽度
**/
function BindDrag(obj, name, viewname, col, arrHeader, callback) {
    obj.addEventListener('mousedown', function(_event) {
        var isMouseDown = false; //初始化状态
        var ele = document.querySelector('#' + name); //获取到td的内容
        var eleLeft = getPointLeft(this); //获取td距离左侧的距离
        var rightPos = eleLeft + ele.offsetWidth + 2; //左侧距离+元素宽度+2(边框)
        //元素右侧端-6px < = 点击的距离   且  点击距离<= 元素右侧端
        if (rightPos - 6 <= _event.pageX && _event.pageX <= rightPos) {
            isMouseDown = true; //如果在右侧边缘偏左6px内.允许移动
        }
        //document的移动时间
        document.onmousemove = function(_event) {
            //如果在范围内
            if (isMouseDown) {
                //改变元素的样式
                ele.classList.remove('cursor');
                ele.classList.add('cursor', 'e-resize');
                //获取移动的宽度
                var width = _event.pageX - eleLeft - 2;
                //获取td的field属性;
                obj.style.width = width + "px";
                var elelist = document.querySelectorAll('[col="' + viewname + col + '"]');
                arrHeader[col].width = width;
                for (var ii = 0; ii < elelist.length; ii++) {
                    elelist[ii].style.width = width + "px";
                }
                callback();
                //var selector = ele.attr('field');
                //查询所有的列元素,改变宽度    
                //$("td[field='" + selector + "']").find('.datagrid-cell').css('width', width);
            }
            //最后返回false;
            return false;
        };
        //鼠标松开清空所有事件
        document.onmouseup = function() {
            document.onmousemove = null;
            document.onmouseup = null;
        };
        //mouseDown返回也要false;
        return false;
    });
}

function getPointLeft(obj) { //获取某元素以浏览器左上角为原点的坐标
    var t = obj.offsetTop; //获取该元素对应父容器的上边距
    var l = obj.offsetLeft; //对应父容器的上边距
    //判断是否有父容器，如果存在则累加其边距
    while (obj = obj.offsetParent) { //等效 obj = obj.offsetParent;while (obj != undefined)
        t += obj.offsetTop; //叠加父容器的上边距
        l += obj.offsetLeft; //叠加父容器的左边距
    }
    return l;

}
